/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import util.*;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import midlet.Mearch;
import view.AppForm;
import view.WaitForm;

/**
 *
 * @author Victorino
 */
public class J2meAppHelper extends AppHelper {

    public static J2meAppHelper instance = new J2meAppHelper();

    public static J2meAppHelper getInstance() {
        
        return instance;
    }

    public Mearch getMidlet() {
        return (Mearch)super.getMidletDefault();
    }
    
    private J2meAppHelper() {
    }

    public void changeForm(AppForm appForm, Object parameter) {
        changeForm(null, appForm, parameter);
    }

    public void changeForm(AppForm appForm) {
        changeForm(null, appForm, null);
    }

    public void changeForm(AppForm appForm, boolean showWait) {
        changeForm(null, appForm, null, showWait);
    }

    public void changeForm(Alert alert, AppForm appForm) {
        changeForm(alert, appForm, null);
    }

    public void changeForm(Alert alert, AppForm form, Object parameter) {

        form.initialize(parameter);

        Display display = Display.getDisplay(midlet);
        if (alert == null) {
            display.setCurrent(form.getDisplyable());
        } else {
            display.setCurrent(alert, form.getDisplyable());
        }
//        System.gc();
    }

    public void changeForm(final Alert alert, final AppForm form, final Object parameter, boolean showWaitForm) {

        final Display display = Display.getDisplay(midlet);

        if (showWaitForm) {
            changeForm(new WaitForm(), "Aguarde...");
        }

        new Thread(new Runnable() {

            public void run() {
                form.initialize(parameter);

                if (alert == null) {
                    display.setCurrent(form.getDisplyable());
                } else {
                    display.setCurrent(alert, form.getDisplyable());
                }
                System.gc();
            }
        }).start();
    }

    public void changeForm(final AppForm form, final Object parameter, final boolean showWaitForm) {
        changeForm(null, form, parameter, showWaitForm);
    }
}
